package com.yugao.fintech.antelope.codegen.assembly;

import com.yugao.fintech.antelope.codegen.core.TableIndexDefinition;
import com.yugao.fintech.antelope.codegen.model.ColumnUnit;
import com.yugao.fintech.antelope.codegen.model.TableColumn;
import com.yugao.fintech.antelope.codegen.model.entity.DatasourceConfig;
import com.yugao.fintech.antelope.codegen.model.entity.GenTable;
import com.yugao.fintech.antelope.codegen.model.entity.GenTableColumn;
import com.yugao.fintech.antelope.codegen.model.entity.GenTableIndex;
import com.yugao.fintech.antelope.codegen.model.rqrs.*;
import com.yugao.fintech.antelope.codegen.template.TmplColumnDefinition;
import com.yugao.fintech.antelope.codegen.template.TmplCsharpColumnDefinition;
import com.yugao.fintech.antelope.codegen.template.TmplJavaColumnDefinition;
import com.yugao.fintech.antelope.codegen.template.TmplTableIndexDefinition;
import com.yugao.fintech.antelope.plugin.gen.model.rqrs.*;
import com.yugao.fintech.framework.assistant.utils.StringUtils;

import java.util.List;

@Mapper(componentModel = "spring", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface GenTableAssembly {
    GenTableColumn from(TableColumn tableColumn);
    GenTableRS toGenTableRS(GenTable genTable);

    void updateGenTableColumn(ColumnUnit columnUnit, @MappingTarget GenTableColumn genTableColumn);

    TmplJavaColumnDefinition toTmplJavaColumnDefinition(TmplColumnDefinition tmplColumnDefinition);

    TmplCsharpColumnDefinition toTmplCsharpColumnDefinition(TmplColumnDefinition tmplColumnDefinition);

    @Mappings({
            @Mapping(source = "columnComment", target = "comment"),
            @Mapping(source = "columnLength", target = "length"),
            @Mapping(source = "columnScale", target = "scale"),
            @Mapping(source = "columnType", target = "type"),
    })
    TmplColumnDefinition toTmplColumnDefinition(GenTableColumn genTableColumn);

    GenTableColumnRS toGenTableColumnRS(GenTableColumn genTableColumn);

    GenTableIndexRS toGenTableIndexRS(GenTableIndex genTableIndex);

    GenTableIndex toGenTableIndex(GenTableIndexRQ genTableIndexRQ);

    void updateGenInfoRS(GenTable genTable, @MappingTarget GenInfoRS genInfoRS);

    void updateGenInfoRS(DatasourceConfig datasourceConfig, @MappingTarget GenInfoRS genInfoRS);

    @Mappings({
            @Mapping(source = "columnNames", target = "columnNames", qualifiedByName = "toList")
    })
    TmplTableIndexDefinition toTmplTableIndexDefinition(GenTableIndex genTableIndex);

    @Named("toList")
    default List<String> toList(String str) {
        return StringUtils.toList(str, ",", String.class);
    }

    GenTableIndex toGenTableIndex(TableIndexDefinition item);
}
